import { useState, useEffect } from 'react';
import { message } from 'antd';
import { history } from 'umi';

import request from '@/utils/request';

/**
 * 获取调岗详情
 * @param processTemplateId
 * @param processInstanceId
 * @param transferPostId
 * @param user
 * @param isOther
 * @returns {{loading: *, detail: *}}
 */
export const useDetail = ({ processTemplateId, processInstanceId, transferPostId, user, isOther, type }) => {
    const [loading, setLoading] = useState(false);
    const {
        employeeName,
        id: employeeId,
        companyId,
        companyName,
        departmentId,
        departmentName,
        postName,
        postId,
        postGradeCode
    } = user;
    const [detail, setDetail] = useState({
        processTaskInstances: [],
        processTaskTemplates: [],
        currentProcessTaskInstance: {
            relatedBusinessObject: {}
        }
    });
    useEffect(() => {
        const getProcess = async () => {
            setLoading(true);
            let url = '/api/oa/v1/transferposts/personal/detail';
            if (type === 'company') {
                url = '/api/oa/v1/transferposts/detail';
            }
            const response = await request(url, {
                method: 'POST',
                data: {
                    processTemplateId,
                    processInstanceId,
                    transferPostId: window.parseInt(transferPostId)
                }
            });
            if (response.code === 10000) {
                if (transferPostId) {
                    setDetail(response.data);
                } else if (isOther) {
                    setDetail({
                        processTaskInstances: [],
                        processTaskTemplates: response.data.processTaskTemplates,
                        currentProcessTaskInstance: {
                            relatedBusinessObject: {
                                applyCompanyId: companyId
                            }
                        }
                    });
                } else {
                    setDetail({
                        processTaskInstances: [],
                        processTaskTemplates: response.data.processTaskTemplates,
                        currentProcessTaskInstance: {
                            relatedBusinessObject: {
                                employeeName,
                                employeeId,
                                companyId,
                                companyName,
                                preDepartmentId: departmentId,
                                preDepartmentName: departmentName,
                                prePostName: postName,
                                prePostId: postId,
                                postGradeCode
                            }
                        }
                    });
                }
            } else {
                message.error(response.msg);
                history.push('/personal/message/system');
            }
            setLoading(false);
        };

        getProcess();
    }, []);

    return {
        loading,
        detail
    };
};
